package XMLearn;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;

public class Test
{
	public static void main(String[] args) throws DocumentException
	{
		List<Person> lps = new ArrayList<Person>();
		SAXReader sr = new SAXReader();
		Document read = sr.read("./p1.xml");
//		获得根元素
		Element root = read.getRootElement();
//		获得指定名称的所有子元素。可以不指定名称
//		List<Node> elements = root.selectNodes("/people/person/name");
		List<Element> list = root.elements();
		for(Element e : list)
		{
			String id = e.elementText("id");
			String name = e.elementText("name");
			int age = Integer.parseInt(e.elementText("age"));
			String sex = e.elementText("sex");
			lps.add(new Person(id, age, name, sex));
		}
		System.out.println(lps);
	}
}
